import { Connection, createConnection } from 'mariadb'

import { DbOptions } from '../../services/dbOptions'

export class MariaDbUtil {
	public static async getConnection(dbOptions: DbOptions): Promise<Connection> {
		if (dbOptions.databaseUrl) {
			return createConnection(dbOptions.databaseUrl)
		}
		return createConnection({
			host: dbOptions.host,
			port: dbOptions.port,
			user: dbOptions.user,
			password: dbOptions.password,
			database: dbOptions.database,
		})
	}

	static async query(conn: Connection, sql: string): Promise<any> {
		return conn.query(sql)
	}
}
